package org.campuscontent.ui;

import java.util.ArrayList;

import javax.swing.JScrollPane;

import org.campuscontent.config.Config;
import org.campuscontent.main.MainFrame;
import org.campuscontent.model.AllocTable;
import org.campuscontent.util.GraphInspector;

public class Template12 extends TemplateBase
{
	public static int paraID = 12;
	
	public Template12(MainFrame mf)
	{
		super(mf);
		alloc = new AllocTable(mf);
		allocScrollPane = new JScrollPane(alloc); 
		allocScrollPane.setBounds(5, 5, 600, 130);

		left.add(allocScrollPane);
	}


	public void initView()
	{
		mf.rootPanel.setDividerLocation(Config.ROOT_HEIGHT);
		this.setDividerLocation(Config.POSITION_TO_HIDE_PROBLEM_CONTROLS);
	}

	public String getAnswer()
	{
		return "";
	}
	
	public boolean setPreview()
	{
		if (!super.setPreview()) return false;
		mf.rootPanel.setDividerLocation(Config.POSITION_TO_SHOW_ALLOC);
		return true;
	}

	public boolean exitPreview()
	{
		super.exitPreview();
		return true;
	}

	public String checkAns()
	{
		graphInspector 		= new GraphInspector( mf.ae.getGraph());
		String allRowsNow   	= graphInspector.getTransitionListAsAllrows();
		String allRowsUser   	= alloc.getAllRows();
		
		if (allRowsNow.equals(allRowsUser)) return "true";
		else
		{
			String now[] = allRowsNow.split(Config.ALLOC_TABLE_SPLITOR);
			String user[] = allRowsUser.split(Config.ALLOC_TABLE_SPLITOR);
			
		    ArrayList<String> nowArray = new ArrayList<String>(java.util.Arrays.asList(now));
		    ArrayList<String> userArray = new ArrayList<String>(java.util.Arrays.asList(user));
		    
		    if (nowArray.size()!=userArray.size()) return "false";
		    else
		    {
			    ArrayList<String> tmpArray = nowArray;
			    
			    nowArray.retainAll(userArray);
			    userArray.removeAll(tmpArray);
			    
			    if (nowArray.equals(tmpArray) && userArray.size() == 0)   //two rows equal
			    	return "true";
			    else
			    	return "false";
		    }
		}
	}

	public void showSolution()
	{
		super.showSolution();
		alloc.deleteAllRows();
		
		graphInspector = new GraphInspector( mf.ae.getGraph());
        alloc.setAllRowToAllocTable(graphInspector.getTransitionListAsAllrows());
        alloc.getMainFrame().templateBase.allocScrollPane.validate();
	}
	
	public boolean updateToLObjectNew()
	{
//		//first check if the user have set the 
//		sAnswer = getAnswer();
//		if (sAnswer.equals("none#none#none#none#"))
//		{
//			JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You should set the answer!");
//			return false;
//		} else
		{
			super.updateToLObjectNew();
			return true;
		}
	}
	public int getId()
	{
		return paraID;
	}
	
}
